package com.cictec.middleware.tde.rm.protocol.rm.decode;

import java.nio.ByteBuffer;

import org.apache.mina.core.session.IoSession;

import com.cictec.middleware.commons.utils.BinaryUtils;
import com.cictec.middleware.tde.exception.MessageDecoderException;
import com.cictec.middleware.tde.rm.protocol.AbstractMessageDecoder;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage.Header;
import com.cictec.middleware.tde.rm.protocol.annotion.TerminalMessageDecoder;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x01TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.utils.RmBinaryUtils;

/**
 * 终端连接请求指令解码
 * @author Zhibin
 *
 */
@TerminalMessageDecoder(0x01)
public class Rm0x01Decoder extends AbstractMessageDecoder {

	@Override
	public TerminalMessage decode(Header header, ByteBuffer in, IoSession session) throws MessageDecoderException {
		
		Rm0x01TerminalMessage msg = new Rm0x01TerminalMessage(header);
		msg.setCreateTime(RmBinaryUtils.dwordToDateRM(in));//生产时间4字节
		msg.setSerialNumber(BinaryUtils.bytesToString(in, 10));//出厂序列号10字节
		msg.setLastCloseReason(BinaryUtils.unsignedByteToInt(in));//断开原因1字节
		msg.setSim(BinaryUtils.bytesToString(in, 8));
		msg.setDriverId(BinaryUtils.dwordToLong(in));//驾驶员id 4字节
		msg.setRestartTimes(BinaryUtils.wordToInt(in));//重启次数2字节
		msg.setNetConnectTimes(BinaryUtils.wordToInt(in));//网络连接次数2字节
		msg.setReserved(BinaryUtils.wordToInt(in));//预留2字节
		
		return msg;
	}

}
